define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, $state, $ionicModal) {
		
		$scope.isIos = false;
		$scope.androidUrl = "http://www.wlwj.com/mobile_apk/apk/wlwj-release.apk";
		$scope.iosUrl = "https://itunes.apple.com/cn/app/%E6%9C%AA%E6%9D%A5%E4%B8%87%E5%AE%B6%E5%95%86%E5%9F%8E/id1263787547?mt=8";
		
		/************模态窗口*************/
		$ionicModal.fromTemplateUrl('templates/modal/downguid.html', {
			scope: $scope,
			animation: 'slide-in-up'
		}).then(function(modal) {
			$scope.modal = modal;
			//是否微信登录
			if($scope.isWeiXin()) {
				var statename = $state.current.name;
				if(statename == "tab.home-appdown") {
					// 提示下载
					$scope.openModal();
				}
			} else {
				if(window.cordova && window.cordova.plugins) {} else {
					if(ionic.Platform.isAndroid()) {
						$scope.isIos = false;
						setTimeout(function(){
							window.location.href = $scope.androidUrl;
						},1500);
					} else {
						$scope.isIos = true;
						setTimeout(function(){
							window.location.href = $scope.iosUrl;
						},1500);
					}
				}
			}
		});
		$scope.openModal = function() {
			$scope.modal.show();
		};
		$scope.closeModal = function() {
			$scope.modal.hide();
		};
		// Cleanup the modal when we're done with it!
		$scope.$on('$destroy', function() {
			$scope.modal.remove();
		});
		/************模态窗口*************/
		//微信内置浏览器
		$scope.isWeiXin = function() {
			var ua = window.navigator.userAgent.toLowerCase();
			if(ua.match(/MicroMessenger/i) == 'micromessenger') {
				return true;
			} else {
				return false;
			}
		}
	}

	controller.$inject = ['$scope', '$state', '$ionicModal'];

	return app.registerController("appdownctrl", controller);
});